package com.tiancikeji.zaoke.app.dao.food.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.tiancikeji.zaoke.app.dao.food.FoodPromotionDao;
import com.tiancikeji.zaoke.app.model.food.FoodPromotion;

public class FoodPromotionDaoImpl extends JdbcDaoSupport implements FoodPromotionDao{

	public List<FoodPromotion> getAllActivePromotionsByDates(Date start, Date end, Integer[] states) {
		String sql = "select `id`, `promotion_date`, `food_id`, `single_margin`, `combo_margin`, `state`, `add_userid`, `add_time`, `lastmod_userid`,`lastmod_time` from `zaoke`.`promotion` where `state` in (";
		sql += StringUtils.join(states, ",");
		sql += ") and `promotion_date` between ? and ?";
		
		return getJdbcTemplate().query(sql, new Object[]{start, end}, new RowMapper<FoodPromotion>() {

			public FoodPromotion mapRow(ResultSet rs, int rowNum) throws SQLException {
				FoodPromotion promotion = new FoodPromotion();
				promotion.setId(rs.getLong("id"));
				promotion.setPromotionDate(rs.getDate("promotion_date"));
				promotion.setFoodid(rs.getLong("food_id"));
				promotion.setSingleMargin(rs.getDouble("single_margin"));
				promotion.setComboMargin(rs.getDouble("combo_margin"));
				promotion.setState(rs.getInt("state"));
				promotion.setAddUserid(rs.getLong("add_userid"));
				promotion.setAddTime(rs.getDate("add_time"));
				promotion.setLastmodUserid(rs.getLong("lastmod_userid"));
				promotion.setLastmodTime(rs.getDate("lastmod_time"));
				return promotion;
			}
		});
	}
}
